package com.haiyi.software.app.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.haiyi.software.app.ActivityPersonal;
import com.haiyi.software.app.ActivityReport;
import com.haiyi.software.app.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public abstract class HiChatMenuBasic extends HiChatCameraBasic {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
	}

	private final static String TAG = "ActivityPersonalBasicMenu";

	private PopupWindow popupWindow;

	private View view;

	private ListView lv_group;

	private List<Map<String, String>> groups;

	private String[] ary = { "查看个人信息" };

	private Button button;

	private Context _context;

	protected void showMenu(Context context, View v) {

		button = (Button) v;

		_context = context;

		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.popup_menu, null);

			lv_group = (ListView) view.findViewById(R.id.list_menu);

			// 加载数据
			groups = new ArrayList<Map<String, String>>();

			for (String item : ary) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("textView", item);
				groups.add(map);
			}

			String[] from = { "textView" };

			int[] to = { R.id.textView_menu };

			SimpleAdapter adpt = new SimpleAdapter(context, groups,
					R.layout.item_menu, from, to);

			lv_group.setAdapter(adpt);

			popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
		}

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		popupWindow.showAsDropDown(button);

		lv_group.setOnItemClickListener(itemClick);
	}

	private OnItemClickListener itemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO 自动生成的方法存根
			TextView txtvView = (TextView) view;

			Log.i(TAG, txtvView.getText().toString());

			if (txtvView.getText().toString().equals("查看个人信息")) {

				Intent intent = new Intent();
				intent.setClass(_context, ActivityPersonal.class);
				Bundle theUID = new Bundle();
				theUID.putString("uid", button.getTag().toString());
				intent.putExtras(theUID);
				startActivity(intent);
			}

			popupWindow.dismiss();

		}
	};
}
